package com.papper.common.service.sys;

import com.papper.common.bean.sys.SysAuth;
import com.papper.common.cache.CacheConstants;
import com.papper.common.cache.redis.CacheClosureNullDefault;
import com.papper.common.mapper.sys.SysAuthMapper;
import com.papper.common.service.ServiceImpl;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @Author:LaJiao
 * @Date:2017-08-03 14:31:08
 * @Description:系统授权
 */
@Service("sysAuthService")
public class SysAuthService extends ServiceImpl<SysAuthMapper, SysAuth> {
	@Override
	protected void clearCaches(SysAuth entity) {
		super.clearCaches(entity);
		cache.delete(CacheConstants.Keys(getModel(), entity.getAuthId()));
	}

	/**
	 * 根据授权ID返回授权信息
	 *
	 * @param authId the auth id
	 * @return the sys auth
	 */
	public SysAuth findByAuthId(String authId) {
		return cache.getAndSet(new CacheClosureNullDefault<SysAuth>() {
			@Override
			public String getKey() {
				return CacheConstants.Keys(getModel(), authId);
			}

			@Override
			public SysAuth getValue() {
				Example          example  = new Example(SysAuth.class);
				Example.Criteria criteria = example.createCriteria();
				criteria.andEqualTo("authId", authId);
				return findOne(example);
			}

			@Override
			public Class<SysAuth> getTypeReference() {
				return SysAuth.class;
			}
		});
	}
}